package com.webapp.controllers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.models.FormBean;

/**
 * Servlet implementation class RegisterServlet
 */

public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super(); 
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// get the session object
		HttpSession session = request.getSession();
		
		//get the parameter email from the html form from request object
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String confirmpassword = request.getParameter("confirmpassword");
		
		String salutation =  request.getParameter("salutation"); // Not needed for validation.
		String name =  request.getParameter("name");
		String language =  request.getParameter("language");
		String middlename =  request.getParameter("middlename"); // Not needed for validation.
		String companyindustry =  request.getParameter("companyindustry");
		String lastname =  request.getParameter("lastname");
		String jobttitle =  request.getParameter("jobtitle");
		String bPhone =  request.getParameter("bPhone");
		String jobRole =  request.getParameter("jobRole");
		String company2 =  request.getParameter("company2");
		String getproducts =  request.getParameter("getproducts"); // Not needed for validation.
		String country =  request.getParameter("country");
		String city =  request.getParameter("city");
		String company3 =  request.getParameter("company3");  // Not needed for validation.
		String state =  request.getParameter("state");
		String address =  request.getParameter("address"); 
		String postal = request.getParameter("postal");
		String address2 =  request.getParameter("address2"); // Not needed for validation.
		String bPhone2 =  request.getParameter("bPhone2");
		String sendPromo =  request.getParameter("sendPromo");

		
		// create the new form bean
		FormBean formb = new FormBean();
		formb.setConfirmpassword(confirmpassword);
		formb.setSalutation(salutation);
	
		formb.setEmail(email);
		formb.setPassword(password);
		formb.setName(name);
		formb.setLanguage(language);
		formb.setMiddlename(middlename);
		formb.setCompanyindustry(companyindustry);
		formb.setLastname(lastname);
		formb.setJobttitle(jobttitle);
		formb.setbPhone(bPhone);
		formb.setJobRole(jobRole);
		formb.setCompany2(company2);
		formb.setGetproducts(getproducts);
		formb.setCountry(country);
		formb.setCity(city);
		formb.setCompany3(company3);
		formb.setState(state);
		formb.setAddress(address);
		formb.setAddress2(address2);
		formb.setPostal(postal);
		formb.setbPhone2(bPhone2);
		formb.setSendPromo(sendPromo);
		
		//return a list of errors
		Map<String, String> errorMap = new HashMap<String, String>();
		boolean isValid = formb.IsValid(errorMap);
		
		// check to see if there are errors. if no errors then we are good!
		if(isValid) {
			// redirect to new page
			response.sendRedirect("Success.html");
			session.setAttribute("FormData", formb);
		} else {
			// put the list object in the session!
			session.setAttribute("ErrorList", errorMap);
			
			session.setAttribute("FormData", formb);
			
            //get the request dispatcher
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/indexCSE336Results.jsp");  
            
            //forward to the jsp file to display
            dispatcher.forward(request, response);                      
		}
	}

}
